using RxjhServer.DbClss;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Timers;

namespace RxjhServer
{
	public class 攻城Class : IDisposable
	{
		private System.Timers.Timer 时间1;

		private System.Timers.Timer 时间2;

		private System.Timers.Timer 时间3;

		private DateTime kssj;

		private DateTime kssjgj;

		private int kssjint;

		public 攻城Class()
		{
			try
			{
				if (World.jlMsg == 1)
				{
					Form1.WriteLine(1, "EventClass-");
				}
				World.攻城战list.Clear();
				kssj = DateTime.Now.AddMinutes(5.0);
				World.攻城战进程 = 1;
				World.攻城战状态 = 1;
				时间1 = new System.Timers.Timer(60000.0);
				时间1.Elapsed += 时间结束事件1;
				时间1.Enabled = true;
				时间1.AutoReset = true;
				时间结束事件1(null, null);
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "攻城战 EventClass 出错：" + ex?.ToString());
			}
		}

		public void 时间结束事件1(object sender, ElapsedEventArgs e)
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "EventClass_时间结束事件1");
			}
			try
			{
				TimeSpan timeSpan = kssj.Subtract(DateTime.Now);
				int num = (int)timeSpan.TotalSeconds;
				int minutes = timeSpan.Minutes;
				if (num <= 0)
				{
					World.攻城战进程 = 2;
					num = 0;
				}
				kssjint = num;
				foreach (Players value in World.allConnectedChars.Values)
				{
					if (value.人物坐标_地图 != int.Parse(World.攻城战地图坐标[0]))
					{
						if (kssjint / 60 != 0)
						{
							value.发送势力战开始倒计时(kssjint);
							value.系统提示("离攻城战开始及参加申请还剩下[" + (kssjint / 60).ToString() + "]分钟.到银币广场，门主;申请 成员;参加", 10, "系统提示");
						}
						else
						{
							value.系统提示("离攻城战开始及参加申请不足一分钟。到银币广场，门主;申请 成员;参加", 10, "系统提示");
						}
					}
				}
				if (kssjint <= 0)
				{
					时间1.Enabled = false;
					时间1.Close();
					时间1.Dispose();
					World.攻城战进程 = 3;
					kssjgj = DateTime.Now.AddMinutes(World.攻城时间);
					时间2 = new System.Timers.Timer(60000.0);
					时间2.Elapsed += 时间结束事件2;
					时间2.Enabled = true;
					时间2.AutoReset = true;
					foreach (攻城战 value2 in World.攻城战list.Values)
					{
						foreach (Players value3 in World.allConnectedChars.Values)
						{
							if (value3.帮派Id == value2.申请帮派ID && value3.帮派人物等级 == 6 && value3.人物坐标_地图 != int.Parse(World.攻城战地图坐标[1]))
							{
								value3.移动(int.Parse(World.攻城战地图坐标[1]), int.Parse(World.攻城战地图坐标[2]), 15f, int.Parse(World.攻城战地图坐标[0]));
								string sqlCommand = $"UPDATE  TBL_帮派数据 SET 名声=名声+{1} WHERE G_Name='{value3.帮派名字}'";
								DBA.ExeSqlCommand(sqlCommand, "GameServer");
								value3.读帮派数据();
								value3.查帮派();
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "攻城战 时间结束事件1 出错：" + ex?.ToString());
			}
		}

		public void 时间结束事件2(object sender, ElapsedEventArgs e)
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "EventClass_时间结束事件2");
			}
			try
			{
				TimeSpan timeSpan = kssjgj.Subtract(DateTime.Now);
				int minutes = timeSpan.Minutes;
				int num = kssjint = (int)timeSpan.TotalSeconds;
				foreach (Players value in World.allConnectedChars.Values)
				{
					if (value.人物坐标_地图 == int.Parse(World.攻城战地图坐标[0]))
					{
						value.发送势力战开始倒计时(num);
						value.系统提示("攻城结束还剩下[" + minutes.ToString() + "]分钟。请努力为你行会争取到城池!", 20, "系统提示");
					}
				}
				if (num <= 0)
				{
					时间2.Enabled = false;
					时间2.Close();
					时间2.Dispose();
					World.攻城战进程 = 4;
					时间3 = new System.Timers.Timer(30000.0);
					时间3.Elapsed += 时间结束事件3;
					时间3.Enabled = true;
					时间3.AutoReset = false;
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "攻城战 时间结束事件2 出错：" + ex?.ToString());
			}
		}

		public void 时间结束事件3(object sender, ElapsedEventArgs e)
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "EventClass_时间结束事件3");
			}
			try
			{
				DBA.ExeSqlCommand("DELETE FROM 攻城城主");
				int minKey = World.攻城战list.OrderByDescending(delegate(KeyValuePair<int, 攻城战> d)
				{
					KeyValuePair<int, 攻城战> keyValuePair4 = d;
					return keyValuePair4.Value.攻城分数;
				}).Select(delegate(KeyValuePair<int, 攻城战> d)
				{
					KeyValuePair<int, 攻城战> keyValuePair3 = d;
					return keyValuePair3.Key;
				}).FirstOrDefault();
				int[] source = World.攻城战list.Where(delegate(KeyValuePair<int, 攻城战> d)
				{
					KeyValuePair<int, 攻城战> keyValuePair2 = d;
					return keyValuePair2.Value.攻城分数 == World.攻城战list[minKey].攻城分数;
				}).Select(delegate(KeyValuePair<int, 攻城战> d)
				{
					KeyValuePair<int, 攻城战> keyValuePair = d;
					return keyValuePair.Key;
				}).ToArray();
				if (source.Count() >= 2)
				{
					World.发送公告("攻城战结束出现了分数相同，活动即将重新开启请大家加油~~到底谁才是最后的霸主？");
					Form1.WriteLine(1, "出现了分数相同的帮派，请或从新手动开启！");
					时间3.Enabled = false;
					时间3.Close();
					时间3.Dispose();
					Dispose();
				}
				else
				{
					foreach (Players value2 in World.allConnectedChars.Values)
					{
						if (minKey == value2.帮派Id && value2.帮派人物等级 == 6)
						{
							World.发送公告("攻城战结束[" + value2.帮派名字 + "]门派获得胜利【" + value2.UserName + "】获得本次城主");
							string msg = string.Format(World.获得胜利内容, World.攻城战list[minKey].攻城分数, World.攻城战list[minKey].申请帮派名称);
							World.发送公告(msg);
							string sqlCommand = $"INSERT INTO 攻城城主 (城主名字,攻城行会名,帮派ID)values('{value2.UserName}','{value2.帮派名字}',{value2.帮派Id})";
							DBA.ExeSqlCommand(sqlCommand);
							value2.加载攻城数据();
							DateTime now = DateTime.Now;
							DateTime dateTime2 = value2.攻城时间 = DateTime.Now.AddDays(World.攻城奖励时间天数);
							DateTime dateTime3 = dateTime2;
							DateTime dateTime4 = dateTime3;
							DateTime dateTime5 = dateTime4;
							DateTime dateTime6 = dateTime5;
							DateTime dateTime7 = dateTime6;
							DateTime dateTime8 = dateTime7;
							now = dateTime8;
							value2.保存人物的数据();
							value2.系统提示("你的攻城奖励到期时间是:" + value2.攻城时间.ToString("yyyy年MM月dd日 hh时mm分"), 21, "系统提示");
						}
						else if (minKey == value2.帮派Id && value2.帮派人物等级 != 6)
						{
							DateTime now2 = DateTime.Now;
							DateTime dateTime2 = value2.攻城时间 = DateTime.Now.AddDays(World.攻城奖励时间天数);
							DateTime dateTime10 = dateTime2;
							DateTime dateTime11 = dateTime10;
							DateTime dateTime12 = dateTime11;
							DateTime dateTime13 = dateTime12;
							DateTime dateTime14 = dateTime13;
							DateTime dateTime15 = dateTime14;
							now2 = dateTime15;
							value2.保存人物的数据();
							value2.系统提示("你的攻城奖励到期时间是:" + value2.攻城时间.ToString("yyyy年MM月dd日 hh时mm分"), 21, "系统提示");
						}
					}
					World.攻城战list.Clear();
					foreach (Players value3 in World.allConnectedChars.Values)
					{
						if (value3.人物坐标_地图 == int.Parse(World.攻城战地图坐标[0]) && value3.帮派Id == minKey)
						{
							if (World.攻城战是否给增幅 == 1 && !value3.追加状态列表.ContainsKey(1008000212))
							{
								string[] array = World.攻城战增幅.Split(';');
								追加状态类 追加状态类 = new 追加状态类(value3, 86400000 * World.攻城战BUFF有效期, 1008000212, 1);
								value3.追加状态列表.Add(追加状态类.FLD_PID, 追加状态类);
								value3.addFLD_追加百分比_攻击(double.Parse(array[0]));
								value3.addFLD_追加百分比_防御(double.Parse(array[1]));
								value3.人物追加最大_HP += int.Parse(array[2]);
								value3.FLD_人物_追加_物品掉落概率百分比 += double.Parse(array[3]);
								value3.FLD_人物_追加_经验百分比 += double.Parse(array[4]);
								value3.状态效果(BitConverter.GetBytes(1008000212), 1, 86400000 * World.攻城战BUFF有效期);
								value3.攻城BUFF++;
								value3.更新武功和状态();
								value3.保存人物的数据();
							}
							if (World.攻城战奖励类型 == 1)
							{
								string[] array2 = World.攻城战奖励属性.Split(';');
								string[] array3 = World.升级会员需要属性.Split(',');
								if (int.Parse(array2[0]) != 0)
								{
									value3.人物_VIP_HP += int.Parse(array2[0]);
									value3.系统提示("生命" + long.Parse(array2[0]).ToString(), 10, "攻城战奖励");
								}
								if (long.Parse(array2[1]) != 0L && value3.FLD_VIP_攻击 < long.Parse(array3[0]))
								{
									value3.FLD_VIP_攻击 += int.Parse(array2[1]);
									value3.系统提示("攻击" + long.Parse(array2[1]).ToString(), 10, "攻城战奖励");
								}
								if (long.Parse(array2[2]) != 0L && value3.FLD_VIP_防御 < long.Parse(array3[1]))
								{
									value3.FLD_VIP_防御 += int.Parse(array2[2]);
									value3.系统提示("防御" + long.Parse(array2[2]).ToString(), 10, "攻城战奖励");
								}
								if (int.Parse(array2[3]) != 0)
								{
									value3.FLD_VIP_回避 += int.Parse(array2[3]);
									value3.系统提示("回避" + int.Parse(array2[3]).ToString(), 10, "攻城战奖励");
								}
								if (int.Parse(array2[4]) != 0)
								{
									value3.人物_VIP_MP += int.Parse(array2[4]);
									value3.系统提示("内功" + int.Parse(array2[4]).ToString(), 10, "攻城战奖励");
								}
								if (long.Parse(array2[5]) != 0L && value3.FLD_RXPIONTX != World.人物最大元宝数)
								{
									value3.查百宝阁元宝数();
									value3.百宝阁检察元宝数据(int.Parse(array2[5]), 1);
									value3.系统提示("赠送" + long.Parse(array2[5]).ToString() + "个元宝！", 10, "攻城战奖励");
									value3.保存元宝数据();
								}
								if (long.Parse(array2[6]) != 0L && value3.FLD_RXPIONT != World.人物最大元宝数)
								{
									value3.查百宝阁元宝数();
									value3.百宝阁检察积分数据(int.Parse(array2[6]), 1);
									value3.系统提示("赠送" + long.Parse(array2[6]).ToString() + "个积分！", 10, "攻城战奖励");
									value3.保存积分数据();
								}
								if (int.Parse(array2[7]) != 0)
								{
									value3.Player_WuXun += int.Parse(array2[7]);
									value3.系统提示("胜利者每人" + int.Parse(array2[7]).ToString() + "点的武勋值", 10, "攻城战奖励");
									value3.更新武功和状态();
								}
								if (int.Parse(array2[8]) != 0)
								{
									if (World.allConnectedChars.TryGetValue(value3.人物全服ID, out Players _))
									{
										DateTime now3 = DateTime.Now;
										now3 = DateTime.Now.AddDays(int.Parse(array2[8]));
										value3.FLD_VIP = 1;
										value3.FLD_VIPTIM = now3;
										value3.保存会员数据();
									}
									value3.系统提示("恭喜你获得" + int.Parse(array2[8]).ToString() + "天会员,请小退后查收！", 10, "攻城战奖励");
								}
							}
							else if (World.攻城战奖励类型 == 2)
							{
								int num = value3.得到包裹空位(value3);
								if (num == -1)
								{
									value3.系统提示("请把背包留一个空位,送你一个套装！", 10, "攻城战奖励");
									return;
								}
								byte[] bytes = BitConverter.GetBytes(RxjhClass.GetDBItmeId());
								byte[] 物品属性 = new byte[20];
								value3.增加物品(bytes, BitConverter.GetBytes(World.攻城战奖励套装), num, BitConverter.GetBytes(1), 物品属性, 绑定: false);
								value3.系统提示("恭喜你获得礼包一份,请查收！", 10, "攻城战奖励");
							}
							else if (World.攻城战奖励类型 == 3)
							{
								string[] array4 = World.攻城战奖励物品.Split(';');
								int num2 = value3.得到包裹空位(value3);
								if (num2 == -1)
								{
									value3.系统提示("请把背包留一个空位,送你一件神秘礼物哦！", 10, "攻城战奖励");
									return;
								}
								value3.增加物品带属性(int.Parse(array4[0]), num2, int.Parse(array4[1]), int.Parse(array4[2]), int.Parse(array4[3]), int.Parse(array4[4]), int.Parse(array4[5]), int.Parse(array4[6]), int.Parse(array4[7]));
								value3.系统提示("恭喜你获得系统奖励物品一件,请查收！", 10, "攻城战奖励");
							}
						}
					}
					时间3.Enabled = false;
					时间3.Close();
					时间3.Dispose();
					Dispose();
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "攻城战 时间结束事件3 出错：" + ex?.ToString());
			}
		}

		public void Dispose()
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "EventClass-Dispose");
			}
			World.攻城战进程 = 0;
			if (时间1 != null)
			{
				时间1.Enabled = false;
				时间1.Close();
				时间1.Dispose();
			}
			if (时间2 != null)
			{
				时间2.Enabled = false;
				时间2.Close();
				时间2.Dispose();
			}
			if (时间3 != null)
			{
				时间3.Enabled = false;
				时间3.Close();
				时间3.Dispose();
			}
			World.攻城战list.Clear();
			foreach (Players value in World.allConnectedChars.Values)
			{
				if (value.人物坐标_地图 == int.Parse(World.攻城战地图坐标[0]))
				{
					value.移动(10f, 10f, 15f, 1201);
				}
			}
			World.攻城 = null;
			World.攻城战状态 = 0;
		}
	}
}
